<?php

class MemberController extends Plugin {
	
    public function __construct() {
        parent::__construct();
		if (empty($this->memberinfo)) $this->redirect(url('member/login', array('back'=>urlencode($_SERVER['HTTP_REFERER']))));
    }
	
	/*
	 * 
	 */
	public function addAction() {
		$id    = (int)$this->get('id');
	    $data  = $this->content->find($id);
	    if (empty($data)) $this->adminMsg('该插件不存在');
	    $catid = $data['catid'];
	    $cat   = $this->cats[$catid];
	    $table = $this->model($cat['tablename']);
	    $_data = $table->find($id);
	    $data  = array_merge($data, $_data); //合并主表和附表
	    if ($catid == 13) $this->adminMsg('该插件属于免费插件，无需购买');
		if ($this->isPostForm()) {
			$data = array(
				'cid'		=> $id,
			    'money'     => $data['jiage'],
				'paytype'   => 'tenpay',
				'order_sn'  => create_sn(),
				'userid'    => $this->memberinfo['id'],
				'username'  => $this->memberinfo['username'],
				'addtime'   => time(),
				'ip'        => client::get_user_ip(),
				'status'    => 0,
			);
			$id = $this->buy->insert($data);
			if ($id) $this->redirect(purl('member/recharge', array('id'=>$id)) . '&' . $this->appurl);
			$this->adminMsg('订购失败');
		}
		$this->view->assign($data);
	    $this->view->display('buy');
	}
	
	/*
	 * 确认支付
	 */
	public function rechargeAction() {
	    $id   = (int)$this->get('id');
		$data = $this->buy->find($id);
		if (empty($data)) $this->adminMsg('支付订单不存在！');
		if ($data['userid'] != $this->memberinfo['id'] || $data['username'] != $this->memberinfo['username']) $this->adminMsg('支付身份不正确！');
		if ($this->isPostForm()) {
		    $this->redirect(purl('member/charge', array('id'=>$id)) . '&' . $this->appurl);exit;
		}
		$this->view->assign($data);
		$this->view->display('charge');
	}
	
	/*
	 * 支付
	 */
	public function chargeAction() {
	    $id   = (int)$this->get('id');
		$data = $this->buy->find($id);
		if (empty($data))    $this->adminMsg('支付订单不存在！');
		if ($data['status']) $this->adminMsg('支付订单已经支付过！', url('my/gou') . '&' . $this->appurl);
		if ($data['userid'] != $this->memberinfo['id'] || $data['username'] != $this->memberinfo['username']) $this->adminMsg('支付身份不正确！');
		$this->get_tenpay($data);
	}
	
	/*
	 * 订单
	 */
	public function orderAction() {
	    $id   = (int)$this->get('id');
		$data = $this->pay_account->find($id);
		if (empty($data)) $this->adminMsg('支付订单不存在！');
		if ($data['userid'] != $this->memberinfo['id'] || $data['username'] != $this->memberinfo['username']) $this->adminMsg('支付身份不正确！');
		if ($this->isPostForm()) {
		    $this->redirect(purl('member/charge', array('id'=>$id)));exit;
		}
		$this->view->assign(array(
			'meta_title' => '在线充值-会员中心-' . $this->site['SITE_NAME'],
			'data'       => $data,
			'pay_config' => $this->cache->get('pay_config'),
	    ));
		$this->view->display('member/pay_order');
	}
	
}